/*
 * 物资列表
 * @Author: ljb
 * @Date: 2018-07-12 10:00:24
 * @Last Modified by: ljb
 * @Last Modified time: 2018-08-10 16:57:42
 */
<template>
	<div class="v-admin-material-index">

		<Table
			:columns="columns"
			:loading="loading"
			:data="materials"
			:height="tableHeight()"
			border
			class="personal-scrollbar"
			@on-sort-change="sortable"/>

	</div>
</template>

<script>
import Button from 'iview/src/components/button';
import { computedTableHeight } from 'src/views/mixins/computedTableHeight';

export default {

	name: 'SuppliesIndex',
	mixins: [computedTableHeight],
	props: {
		loading: {
			type: Boolean,
			default() {
				return false;
			},
		},
		materials: {
			type: Array,
			default() {
				return [];
			},
		},
	},

	data() {
		return {
			columns: [
				{
					title: this.$t('supplies_management.name'),
					key: 'name',
					align: 'center',
					render: (h, { row }) => <div> { row.material.name || '-' } </div>,
				},
				{
					title: this.$t('supplies_management.code'),
					key: 'code',
					align: 'center',
					render: (h, { row }) => <div> { row.material.code || '-' } </div>,
				},
				{
					title: this.$t('supplies_management.stock'),
					key: 'stock',
					align: 'center',
					sortable: 'custom',
					render: (h, { row }) => <div> { row.stock || '-' } </div>,
				},
				{
					title: this.$t('supplies_management.specification'),
					key: 'specifications',
					align: 'center',
					render: (h, { row }) => <div> { row.material.specifications || '-' } </div>,
				},
				{
					title: this.$t('supplies_management.producer'),
					key: 'manufacturer',
					align: 'center',
					render: (h, { row }) => <div> { row.material.manufacturer || '-' } </div>,
				},
				{
					title: this.$t('supplies_management.buyer'),
					key: 'buyer',
					align: 'center',
					render: (h, { row }) => <div> { row.buyer || '-' } </div>,
				},
				{
					title: this.$t('supplies_management.buy_num'),
					key: 'sku',
					align: 'center',
					sortable: 'custom',
					render: (h, { row }) => <div> { row.sku || '-' } </div>,
				},
				{
					title: this.$t('supplies_management.remark'),
					key: 'remark',
					align: 'center',
					render: (h, { row }) => <div> { row.remark || '-' } </div>,
				},
				{
					title: this.$t('supplies_management.buy_date'),
					key: 'purchase_at',
					align: 'center',
					sortable: 'custom',
					render: (h, { row }) => <div> { row.purchase_at || '-' } </div>,
				},
				{
					title: this.$t('common.operation'),
					align: 'center',
					width: 180,
					render: (h, { row }) => (
						<div>
							{
								Number(row.stock) !== 0
									? <Button size="small" style={{ marginRight: '5px' }} on-click={() => { this.$emit('sell', row); }} type="primary">{this.$t('supplies_management.sell')}</Button>
									: <Button size="small" style={{ marginRight: '5px' }} type="error" disabled>{this.$t('supplies_management.sell')}</Button>
							}

						</div>),
				},
			],
		};
	},

	methods: {

		/**
         * 排序的回调
         * @param  {Object} param {对应列的属性对象,里面包含了column列属性，key值，order排序字段}
         * @return {undefined}
         */
		sortable(param) {
			const search_param = {
				order_by: param.order === 'normal' ? '' : param.key,

				sort: param.order === 'normal' ? '' : param.order.toLocaleUpperCase(),
			};

			this.$emit('sort', search_param);
		},

	},
};
</script>
